import request from '@/utils/request'
import { AxiosResponse } from 'axios'

export interface DataArray {
  id:number
  createdBy:string
  updatedBy:string
  createdTime: string
  updatedTime: string
  operatorId: null
  parentId: number
  name: string
  href: string
  icon: string
  orderNum: number
  description: string
  shown: boolean
  level: number
  selected: boolean
  subMenuList: null | Array<DataArray>
}

interface MenuList{
  [propName:string]:string | Array<DataArray>
}

interface MenuAdd{
  id?: number
  parentId: number
  name: string
  href: string
  description?: string
  icon?: string
  shown?: boolean
  orderNum?: number
}

interface AllocateRole {
  roleId?:number
  menuIdList?:Array<number>
}

// 获取菜单，并按层级展示
export const getMenuNodeList = ():Promise<AxiosResponse<MenuList>> => {
  return request({
    method: 'GET',
    url: '/boss/menu/getMenuNodeList'
  })
}

// 保存或新增菜单
export const saveOrUpdate = (data:MenuAdd):Promise<AxiosResponse> => {
  return request({
    method: 'POST',
    url: '/boss/menu/saveOrUpdate',
    data
  })
}

// 获取编辑页信息 ，默认 id = -1
export const getEditMenuInfo = (id = -1):Promise<AxiosResponse> => {
  return request({
    method: 'GET',
    url: '/boss/menu/getEditMenuInfo',
    params: { // 相当于 /boss/menu/getEditMenuInfo?id=id
      id
    }
  })
}

export const getAll = ():Promise<AxiosResponse> => {
  return request({
    method: 'GET',
    url: '/boss/menu/getAll'
  })
}

export const deleteMenu = (id:number):Promise<AxiosResponse> => {
  return request({
    method: 'GET',
    url: `/boss/menu/${id}`
  })
}

// 给角色分配菜单
export const allocateRoleMenus = (data:AllocateRole):Promise<AxiosResponse> => {
  return request({
    method: 'POST',
    url: '/boss/menu/allocateRoleMenus',
    data
  })
}

// 获取角色拥有的菜单
export const getRoleMenus = (roleId:number|string):Promise<AxiosResponse> => {
  return request({
    method: 'GET',
    url: '/boss/menu/getRoleMenus',
    params: {
      roleId
    }
  })
}
